/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.service.impl;

import com.minierp.bean.TransferBillBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.BankAccountBalanceDao;
import com.minierp.dao.BankAccountDetailDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.SerialNumberDao;
import com.minierp.dao.TransferBillDao;
import com.minierp.err.DataChangedException;
import com.minierp.err.NotAllowException;
import com.minierp.helper.PageHelper;
import com.minierp.model.SystemLog;
import com.minierp.model.TransferBill;
import com.minierp.service.TransferBillService;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TransferBillServiceImpl implements TransferBillService {
	public void txInsert(TransferBillBean form,
			TransferBillDao transferBillDao, SerialNumberDao serialNumberDao,
			IdGenerator idGenerator, BankAccountBalanceDao bankAccountBalanceDao)
			throws Exception {
		TransferBill obj = form.getTransferBill();
		obj.setCreateTime(new Date());
		obj.setId(idGenerator.getId(TransferBill.class.getName()));
		if (!form.getCodeRule().getGenerateRule().equals("MANUAL")) {
			obj.setBillCode(PageHelper.generateCode(serialNumberDao, form
					.getCodeRule(), "", form.getTransferBill().getTallySetId()));

			for (TransferBill d = transferBillDao.get(obj.getBillCode()); d != null; d = transferBillDao
					.get(obj.getBillCode())) {
				obj.setBillCode(PageHelper.generateCode(serialNumberDao, form
						.getCodeRule(), "", form.getTransferBill()
						.getTallySetId()));
			}
		}

		if (obj.getExamined().equals("Y")) {
			obj.setExamSms("N");
			obj.setExamineTime(new Date());
		} else {
			obj.setExamSms("Y");
		}

		transferBillDao.save(obj);
		if (obj.getExamined().equals("Y")) {
			PageHelper.updateBankAccount(obj.getFromAccountId(),
					obj.getTallySetId(), obj.getId(), obj.getCost(),
					obj.getTransferDate(), obj.getExamineTime(),
					obj.getBillCode(), "TRANSFER_BILL", "", "转账", "P",
					obj.getManagerName(), obj.getMark(), "TRANSFER_OUT",
					"SAVE_EXAM", false, (BankAccountDetailDao) null,
					bankAccountBalanceDao, idGenerator);
			PageHelper.updateBankAccount(obj.getToAccountId(),
					obj.getTallySetId(), obj.getId(), obj.getCost(),
					obj.getTransferDate(), obj.getExamineTime(),
					obj.getBillCode(), "TRANSFER_BILL", "", "转账", "R",
					obj.getManagerName(), obj.getMark(), "TRANSFER_IN",
					"SAVE_EXAM", false, (BankAccountDetailDao) null,
					bankAccountBalanceDao, idGenerator);
		}

	}

	public void txUpdate(TransferBillBean form,
			TransferBillDao transferBillDao, IdGenerator idGenerator,
			BankAccountBalanceDao bankAccountBalanceDao) throws Exception {
		TransferBill obj = form.getTransferBill();
		Object[] sv = transferBillDao.getStatusValues(obj.getId());
		if (sv == null) {
			throw new Exception("Can\'t get status values for transfer bill:"
					+ obj.getId());
		} else {
			long existSycVal = ((Date) sv[0]).getTime();
			String exitEnabled = sv[1].toString();
			String existExamined = sv[2].toString();
			if (existSycVal != form.getSychronizeValue()) {
				throw new DataChangedException("转帐取款单已被其他操作修改，请关闭页面再打开后重新操作！");
			} else if (exitEnabled.equals("N")) {
				throw new NotAllowException("转帐取款单已作废，不能修改！");
			} else if (existExamined.equals("Y")) {
				throw new NotAllowException("转帐取款单已审核，不能修改！");
			} else {
				if (obj.getExamined().equals("Y")) {
					obj.setExamSms("N");
					obj.setExamineTime(new Date());
				} else {
					obj.setExamSms("Y");
				}

				obj.setUpdateTime(new Date());
				transferBillDao.update(obj);
				if (obj.getExamined().equals("Y")) {
					PageHelper.updateBankAccount(obj.getFromAccountId(),
							obj.getTallySetId(), obj.getId(), obj.getCost(),
							obj.getTransferDate(), obj.getExamineTime(),
							obj.getBillCode(), "TRANSFER_BILL", "", "转账", "P",
							obj.getManagerName(), obj.getMark(),
							"TRANSFER_OUT", "SAVE_EXAM", false,
							(BankAccountDetailDao) null, bankAccountBalanceDao,
							idGenerator);
					PageHelper.updateBankAccount(obj.getToAccountId(),
							obj.getTallySetId(), obj.getId(), obj.getCost(),
							obj.getTransferDate(), obj.getExamineTime(),
							obj.getBillCode(), "TRANSFER_BILL", "", "转账", "R",
							obj.getManagerName(), obj.getMark(), "TRANSFER_IN",
							"SAVE_EXAM", false, (BankAccountDetailDao) null,
							bankAccountBalanceDao, idGenerator);
				}

			}
		}
	}

	public void txExamine(long id, TransferBillDao transferBillDao,
			long examinerId, String examinerName,
			BankAccountBalanceDao bankAccountBalanceDao, IdGenerator idGenerator)
			throws Exception {
		TransferBill a = (TransferBill) transferBillDao.get(TransferBill.class,
				id);
		if (a != null && a.getEnabled().equals("Y")
				&& a.getExamined().equals("N")) {
			a.setExamined("Y");
			a.setExamSms("N");
			a.setExamineTime(new Date());
			a.setExaminerId(examinerId);
			a.setExaminerName(examinerName);
			a.setUpdateTime(new Date());
			transferBillDao.update(a);
			PageHelper.updateBankAccount(a.getFromAccountId(),
					a.getTallySetId(), a.getId(), a.getCost(),
					a.getTransferDate(), a.getExamineTime(), a.getBillCode(),
					"TRANSFER_BILL", "", "转账", "P", a.getManagerName(),
					a.getMark(), "TRANSFER_OUT", "EXAM", false,
					(BankAccountDetailDao) null, bankAccountBalanceDao,
					idGenerator);
			PageHelper.updateBankAccount(a.getToAccountId(), a.getTallySetId(),
					a.getId(), a.getCost(), a.getTransferDate(),
					a.getExamineTime(), a.getBillCode(), "TRANSFER_BILL", "",
					"转账", "R", a.getManagerName(), a.getMark(), "TRANSFER_IN",
					"EXAM", false, (BankAccountDetailDao) null,
					bankAccountBalanceDao, idGenerator);
		}

	}

	public void txUnExamine(long id, TransferBillDao transferBillDao,
			BankAccountBalanceDao bankAccountBalanceDao,
			BankAccountDetailDao bankAccountDetailDao, IdGenerator idGenerator,
			UserBean ub) throws Exception {
		TransferBill a = (TransferBill) transferBillDao.get(TransferBill.class,
				id);
		if (a != null && a.getEnabled().equals("Y")
				&& a.getExamined().equals("Y")) {
			a.setExamined("N");
			a.setExamSms("Y");
			a.setExaminerId(0L);
			a.setExaminerName("");
			a.setUpdateTime(new Date());
			transferBillDao.update(a);
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String mark = ub.getStaffer().getStafferName() + "于"
					+ sf.format(new Date()) + "反审核单据";
			PageHelper.updateBankAccount(a.getFromAccountId(),
					a.getTallySetId(), a.getId(),
					(new BigDecimal(0)).subtract(a.getCost()),
					a.getTransferDate(), new Date(), a.getBillCode(),
					"TRANSFER_BILL", "", "转账", "P", a.getManagerName(), mark,
					"TRANSFER_OUT", "UN_EXAM", true, bankAccountDetailDao,
					bankAccountBalanceDao, idGenerator);
			PageHelper.updateBankAccount(a.getToAccountId(), a.getTallySetId(),
					a.getId(), (new BigDecimal(0)).subtract(a.getCost()),
					a.getTransferDate(), new Date(), a.getBillCode(),
					"TRANSFER_BILL", "", "转账", "R", a.getManagerName(), mark,
					"TRANSFER_IN", "UN_EXAM", false, bankAccountDetailDao,
					bankAccountBalanceDao, idGenerator);
		}

	}

	public void txCancel(long id, String reason,
			TransferBillDao transferBillDao, UserBean ub,
			BankAccountBalanceDao bankAccountBalanceDao,
			BankAccountDetailDao bankAccountDetailDao, IdGenerator idGenerator)
			throws Exception {
		TransferBill d = (TransferBill) transferBillDao.get(TransferBill.class,
				id);
		if (d != null && d.getEnabled().equals("Y")) {
			d.setEnabled("N");
			d.setExamSms("N");
			d.setDisableManId(ub.getUser().getStafferId());
			d.setUpdateTime(new Date());
			transferBillDao.update(d);
			if (d.getExamined().equals("Y")) {
				SimpleDateFormat log = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String mark = ub.getStaffer().getStafferName() + "于"
						+ log.format(new Date()) + "作废单据";
				PageHelper.updateBankAccount(d.getFromAccountId(),
						d.getTallySetId(), d.getId(),
						(new BigDecimal(0)).subtract(d.getCost()),
						d.getTransferDate(), new Date(), d.getBillCode(),
						"TRANSFER_BILL", "", "转账", "P", d.getManagerName(),
						mark, "TRANSFER_OUT", "CANC", true,
						bankAccountDetailDao, bankAccountBalanceDao,
						idGenerator);
				PageHelper.updateBankAccount(d.getToAccountId(),
						d.getTallySetId(), d.getId(),
						(new BigDecimal(0)).subtract(d.getCost()),
						d.getTransferDate(), new Date(), d.getBillCode(),
						"TRANSFER_BILL", "", "转账", "R", d.getManagerName(),
						mark, "TRANSFER_IN", "CANC", false,
						bankAccountDetailDao, bankAccountBalanceDao,
						idGenerator);
			}

			SystemLog log1 = new SystemLog();
			log1.setId(idGenerator.getId(SystemLog.class.getName()));
			log1.setActionCode("TRANSFER_BILL");
			log1.setLogType("CANCEL_LOG");
			log1.setCreaterId(ub.getStaffer().getId());
			log1.setCreaterName(ub.getStaffer().getStafferName());
			log1.setDepName(ub.getDepName());
			log1.setDepId(ub.getStaffer().getDepartmentId());
			log1.setStatus("S");
			log1.setUserName(ub.getUser().getUserName());
			log1.setIp(d.getBillCode());
			log1.setMark("");
			log1.setRelId(id);
			log1.setCancelReason(reason);
			log1.setTallySetId(d.getTallySetId());
			transferBillDao.save(log1);
		}

	}
}